package cn.crc.pjblog.pages;


import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry.annotations.ApplicationState;
import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Inject;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.corelib.components.Form;
import org.hibernate.Query;


import cn.crc.model.dao.PublicDAO;
import cn.crc.model.BlogMember;
import cn.crc.pjblog.util.PublicMethod;
import cn.crc.spring.SpringBean;


public class Login {
	
	private PublicMethod pm = PublicMethod.getInstance();
	
	
	private String userName;
	private String password;
	
	@InjectPage	
	private Register register;
	
	@InjectPage
	private Default defaultPage;
	
	@Inject 
	@SpringBean("IPublicDAO")
	private PublicDAO dao;
	
	@Component
	private Form checkUser;
	
	@ApplicationState
	private BlogMember loginBlogMember;	
	private boolean loginBLogMemberExists;
	
	
	public void setLoginBlogMember(BlogMember loginBlogMember) {
		this.loginBlogMember = loginBlogMember;
	}
	public String getPassword() {
		return password;
	}
	public String getUserName() {
		return userName;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	
	/**
	 * @author doublel
	 * @param none
	 * @return  Object 
	 * @exception none
	 *
	 * 点击注册用户后的的处理事件
	 * */
	
	
	void onAny(){
		System.out.println("enter onAny Method ");
		
		
	}
	Object onSelectedFromRegister(){
		
		System.out.println(" enter register");
		return this.register;
		
	}
	void  onSelectedFromLoginSubmit(){
		
		System.out.println("onSelectedFromLoginSubmit");
		
		
	}
	/**
	 * @author doublel
	 * @param String userName,String password
	 * @return  if has error return error message
	 * 
	 * 提交表单checkUser 进行用户名字和密码的验证
	 * */
	
	Object onActionFromCheckUser(){
		
		String userName = this.getUserName();
		String password  =  this.getPassword();
		
		System.out.println("userName="+userName);
		System.out.println("password="+password);
		String hsql = "from BlogMember where MemName='"+ userName +"' and MemPassword='"+ password +"'";
		
//		Query query = this.dao.createQuery(hsql);
//		query.setParameter("memName", userName);
//		query.setParameter("memPassword", password);
		
		List it =this.dao.getDataByHSQL(hsql);
		
		if( it == null || it.size()==0){
			this.checkUser.recordError("用户名或密码错误");
			
		}else if (it.size()==1)
		{
			this.setLoginBlogMember((BlogMember) it.get(0));
			return defaultPage;
			
					
		}else
		{
			this.checkUser.recordError("用户名或密码错误");
		}
		return null;
		
	}
	
}
